﻿using System;
using System.Collections;

//自定义命名空间
using BasicUtility = DreamCube.Foundation.Basic.Utility;
using RemotingFramework = DreamCube.Framework.Utilities.Remoting;

namespace BGN.PlugIn.Server
{
    public class SeverSinfoMsgPlugin : SisenMESS_Interface.SisenMESS_Server_Inteface
    {
        public event SisenMESS_Interface.OnSendEventArgsHandler OnSend;

        public event SisenMESS_Interface.OnSendDataEventArgsHandler OnSendData;

        public string _Company
        {
            get { return Configer.SeverSinfoMsgPluginConfiger.Instance.GetCompanyName(); }
        }

        public string _ID
        {
            get { return Configer.SeverSinfoMsgPluginConfiger.Instance.GetID(); }
        }

        public string _ManageApplicationName
        {
            get { return Configer.SeverSinfoMsgPluginConfiger.Instance.GetManageApplicationName(); }
        }

        public string _Product
        {
            get { return Configer.SeverSinfoMsgPluginConfiger.Instance.GetProduct(); }
        }

        private BGN.Common.Remoting.Msg msgRemotingObj = null;

        public void StartService()
        {
            try
            {
                Int32 port = 0;
                BasicUtility.MyObject.TryToInt32(Configer.SeverSinfoMsgPluginConfiger.Instance.GetXmlConfigNodeInnerText("RemotingPort"), out port);
                RemotingFramework.Server.Instance.InitialServer(port, "SinfoMsgPlugin");

                //注册远程访问对象
                if (msgRemotingObj == null)
                {
                    msgRemotingObj = new  BGN.Common.Remoting.Msg();
                    //当remoting对象接收到发送消息的数据时，调用布谷鸟的通信接口，把短信消息发送到客户端上
                    msgRemotingObj.SendMsgToClient += new Action<SisenMESS_Interface.SisenSendEventArgs>(msgRemotingObj_OnSendMsgToClient);
                }
                RemotingFramework.Server.Instance.RegisterRemotingObjByInstance(msgRemotingObj);
            }
            catch (Exception ex)
            {
                BasicUtility.MyLog.MakeLog(ex);
            }
        }

        //发送消息到客户端上
        void msgRemotingObj_OnSendMsgToClient(SisenMESS_Interface.SisenSendEventArgs e)
        {
            BasicUtility.MyLog.MakeLog("接受到消息：" + System.Text.Encoding.UTF8.GetString(e.SendBytes), BasicUtility.MyLog.LogType.Debug);
            OnSend(this, e);
        }

        public void StopService()
        {
            RemotingFramework.Server.Instance.Stop();
        }

        public void _ReceiveData(Object oj)
        {
        }

        public void _UserOnline(String userid, String user_online_type, String userip)
        {
        }

        public void _UserOutLine(String userid)
        {
        }
    }
}
